package com.zhongyouyun.cqyc.service.imp;

import com.zhongyouyun.cqyc.model.PocketBookModel;
import com.zhongyouyun.cqyc.mapper.PocketBookMapper;
import com.zhongyouyun.cqyc.pojo.PocketBookPojo;
import com.zhongyouyun.cqyc.service.PocketBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.*;

/**
 * @Author: shenyi
 * @Date: 2023-10-20
 * @Description: 口袋书服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class PocketBookServiceImpl extends ServiceImpl<PocketBookMapper, PocketBookModel> implements PocketBookService {
    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 分页查询
     * @param page 分页数据  lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public IPage<PocketBookModel> selectPage(Page<PocketBookModel> page, QueryWrapper<PocketBookModel> queryWrapper) {
        return this.baseMapper.selectPage(page, queryWrapper);
    }


    /*
     * @Author: shenyi
     * @Date: 2021-09-08
     * @Description: 查询全部
     * @param   lambdaQueryWrapper where 条件 和排序
     */
    @Override
    public List<PocketBookModel> selectList(QueryWrapper<PocketBookModel> queryWrapper) {
        return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage<PocketBookPojo> selectPojoPage(Page page, QueryWrapper<PocketBookPojo> queryWrapper) {
        return this.baseMapper.selectPojoPage(page, queryWrapper);
    }

    @Override
    public List<Map> getPocketBookList(String search) {
        List<Map> result = new ArrayList<>();
        QueryWrapper<PocketBookModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("state", "1");
        queryWrapper.like("title", search);
        List<PocketBookModel> pocketBookModels = this.selectList(queryWrapper);
        for (PocketBookModel pocketBookModel : pocketBookModels) {
            Map map1 = new HashMap();
            map1.put("id", pocketBookModel.getId().toString());
            map1.put("title", pocketBookModel.getTitle());
            map1.put("count", "1");
            result.add(map1);
        }
        if (result.size() > 5) {
            List<Map> result1 = new ArrayList<>();
            for (int i = 0; i < 5; i++) {
                result1.add(result.get(i));
            }
            return result1;
        } else {
            return result;
        }
    }
}
